import {
  EntitySubscriberInterface,
  EventSubscriber,
  InsertEvent,
  RemoveEvent,
  UpdateEvent
} from 'typeorm'
import { Inject } from '@nestjs/common'
import { WinstonLogger, WINSTON_MODULE_LOGGER } from '@ttou/nest-winston'
import { UserEntity } from '../entities'

@EventSubscriber()
export class UserSubscriber implements EntitySubscriberInterface<UserEntity> {
  constructor(
    @Inject(WINSTON_MODULE_LOGGER)
    private logger: WinstonLogger
  ) {}

  listenTo() {
    return UserEntity
  }

  async afterInsert(event: InsertEvent<UserEntity>) {
    this.logger.warn('新增用户', UserSubscriber.name)
  }

  async afterUpdate(event: UpdateEvent<UserEntity>) {
    this.logger.warn('更新用户', UserSubscriber.name)
  }

  async afterRemove(event: RemoveEvent<UserEntity>) {
    this.logger.warn('删除删除', UserSubscriber.name)
  }
}
